const FreeMarker = require("freemarker.js");
const path = require("path");
const fs = require("fs");

let fm = new FreeMarker({
    viewRoot: path.join(__dirname + "/template")  //配置模版目录
})

let dataObject = { //往模板里传的数据
    navList: [
        {title: '基本概况',src: './home.html'},
        {title: '关于我们',src: './about.html'},
        {title: '新闻中心',src: './news.html'},
        {title: '教学科研',src: './teacseac.html'},
        {title: '教学安排',src: './teacarran.html'},
        {title: '学习园地',src: './learnplace.html'},
        {title: '合作交流',src: './coocom.html'},
        {title: '联系我们',src: './callus.html'}
    ],
    
    imgList1: [
        {src: '../images/swiper1.png'},
        {src: '../images/swiper2.png'},
        {src: '../images/swiper3.png'},
        {src: '../images/swiper4.png'}
    ],
    
    imgList2: [
        {src: '../images/news1.jpg'},
        {src: '../images/news2.jpg'},
        {src: '../images/news1.jpg'},
        {src: '../images/news2.jpg'}
    ],

    rollNews: Array(6).fill({title: '学员记-传承红色基因 坚定理想信念 不忘初心使命 中国核建党支部书记及党务工作者',src: './'}),
    
    schoolNews: Array(10).fill({title: '学员记-传承红色基因 坚定理想信念 不忘初心使命 中国核建党支部书记及党务工作者',src: './',time: '10-10'}),
    
    covImg: Array(8).fill({title: '政府系统',src: '../images/news1.jpg'}),

    BlockList1: Array(6).fill({title: '情景教学',src: './',time: '10-10'}),
    BlockList2: Array(6).fill({title: '一天教学计划',src: './',time: '10-10'}),
    BlockList3: Array(6).fill({title: '盛世王朝基地',src: './',time: '10-10'}),

    BlockList4: Array(6).fill({title: '青海广播电视大学 “不忘初心使命”',src: './',time: '10-10'}),
    BlockList5: Array(6).fill({title: '毛泽东与延安精神',src: './',time: '10-10'}),
    BlockList6: Array(4).fill({title: '延安市人民政府印发《关于进一步',src: './',time: '10-10'}),

    plcList: Array(8).fill({title: '延安革命纪念碑',src: './'}),

    scrollPics: Array(10).fill({title: '学员记-支部活动',src: '../images/news2.jpg'}),
}

let viewPath = __dirname + '/template' //模板文件夹
let outputPath = __dirname + '/dist' //输出目录
let filesName = fs.readdirSync(viewPath) //读取文件夹下的文件名和目录名（混合在一起）
let dirs = []
let fileStat = ''
filesName.forEach(item => { //遍历拼接路径访问文件或目录，判断类型，将符合要求的存起来
    fileStat = fs.statSync(path.join(viewPath,item))
    if(fileStat.isFile()) {
        dirs.push(item)
    }
})

dirs.forEach(item => {
	fm.render(item, dataObject, function(err, html, output){
	    //将文件用数据渲染并输出新的文件
	    fs.writeFile(path.join(outputPath,item.split('.')[0] + ".html"), html, 'utf8', function(err) {
	        if(err) {
	            console.log('输出错误')
	            return
	        }
	    });
	})
})
